package com.itheima.redbaby.activity;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.itheima.redbaby.BaseActivity;
import com.itheima.redbaby.R;
import com.itheima.redbaby.bean.Filter;
import com.itheima.redbaby.bean.FilterValue;

public class FilterActivity extends BaseActivity implements OnClickListener {
//	private final String TAG = "FilterActivity";
	private final int FILTER_NUMBER = 3;
	
	private List<Filter> list_filter;
	private ListView filter_category;
	private FilterAdapter adapter;
	//父filter的位置,0~2
	private int parent_filter;
	//设置子filter的标题
	private String filter_name;
	//父filter的listview的item的name值，来源于子filter
	private String childfilter_name = "全部";
	private List<FilterValue> filter_values;
	//拼接3个filter的id
	public static Map<Integer, String> Main_Filter = new HashMap<Integer, String>();

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.filt_product_activity);
		
		Intent preIntent = getIntent();
		parent_filter = preIntent.getIntExtra("parent_filter", -1);
		String json = preIntent.getStringExtra("list_filter");
		list_filter = JSONObject.parseArray(json, Filter.class);
		//如果有父filter，则赋值，并在界面显示该父filter的子filter。
		if(parent_filter != -1) {
			Filter filter = list_filter.get(parent_filter);
			filter_name = filter.getKey();
			filter_values = filter.getValue();
		}
		
		init();
	}
	
	private void init() {
		TextView tv_back = (TextView) findViewById(R.id.titleBack);
		TextView tv_title = (TextView) findViewById(R.id.categoryTitle);
		filter_category = (ListView) findViewById(R.id.categoryList);
		
		tv_back.setOnClickListener(this);
		if(TextUtils.isEmpty(filter_name)) {
			tv_title.setText(filter_name);
		}
		
		adapter = new FilterAdapter();
		filter_category.setAdapter(adapter);
		filter_category.setOnItemClickListener(new OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				//子filter该做什么操作
				if(parent_filter != -1) {
					FilterValue filterValue = filter_values.get(position);
					String filterid = filterValue.getId();
					//替换filter属性值
					if(FilterActivity.Main_Filter.containsKey(parent_filter)) {
						FilterActivity.Main_Filter.remove(parent_filter);
					}
					FilterActivity.Main_Filter.put(parent_filter, filterid);
					
					//假如filter属性选择结束，转向商品列表页面
					if(FilterActivity.Main_Filter.size() == FILTER_NUMBER) {
						Intent intent = new Intent(FilterActivity.this, CategoryProductListActivity.class);
						String filter = "";
						for(int i = 0; i < FILTER_NUMBER; i++) {
							filter += FilterActivity.Main_Filter.get(i);
						}
						intent.putExtra("filter", filter);
						startActivity(intent);
						
						//转向商品页面同时，关闭filter父页面
						setResult(RESULT_CANCELED);
					} else {//不转向商品列表页面，而是返回主filter界面
						Intent data = new Intent();
						data.putExtra("pos", parent_filter);
						data.putExtra("name", filterValue.getName());
						setResult(RESULT_OK, data);
					}
					
					finish();
				} else { //父filter该跳到子filter
					Intent intent = new Intent(FilterActivity.this, FilterActivity.class);
					intent.putExtra("parent_filter", position);
					String json = JSONArray.toJSONString(list_filter);
					intent.putExtra("list_filter", json);
					startActivityForResult(intent, 110);
				}
			}
		});
	}
	
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		//从子filter转回父filter时
		if(requestCode == 110) {
			if(resultCode == RESULT_OK) {
				int pos = data.getIntExtra("pos", 0);
				String name = data.getStringExtra("name");
				View filter = filter_category.getChildAt(pos);
				TextView tv_name = (TextView) filter.findViewById(R.id.textName);
				tv_name.setText(name);
				tv_name.setTextColor(Color.RED);
			} else if(resultCode == RESULT_CANCELED) {
				finish();
			}
		}
	}

	class FilterAdapter extends BaseAdapter {
		@Override
		public int getCount() {
			if(parent_filter != -1 && filter_values != null)
				return filter_values.size();
			else
				return list_filter.size();
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			//为根filter
			if(parent_filter == -1) {
				if(convertView == null) {
					convertView = View.inflate(FilterActivity.this, R.layout.filt_item, null);
				}
				Filter filter = list_filter.get(position);
				TextView filter_content = (TextView) convertView.findViewById(R.id.textContent);
				TextView filter_name = (TextView) convertView.findViewById(R.id.textName);
				filter_content.setText(filter.getKey() + "：");
				filter_name.setText(childfilter_name);
				return convertView;
			} else { //不是根filter
				if(convertView == null) {
					convertView = View.inflate(FilterActivity.this, R.layout.filt_item2, null);
				}
				FilterValue filter_value = filter_values.get(position);
				TextView filter_content = (TextView) convertView.findViewById(R.id.textContent);
				filter_content.setText(filter_value.getName());
				return convertView;
			}
		}

		@Override
		public Object getItem(int position) {
			return null;
		}

		@Override
		public long getItemId(int position) {
			return position;
		}
	}
	
	@Override
	public void onClick(View v) {
		//点击了返回按钮
		finish();
	}
}
